<template>
  <div class="login">
    <div class="overlay"></div>
    <div class="cont">
      <div class="title">后台管理系统</div>
      <div class="form">
        <el-form ref="formRef" :model="forms" :rules="rules">
          <el-form-item prop="account">
            <el-input
              v-model="forms.account"
              @keyup.enter="loginSys"
              prefix-icon="User"
              clearable
              placeholder="用户名"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="forms.password"
              @keyup.enter="loginSys"
              prefix-icon="Lock"
              :type="showPwd ? 'text' : 'password'"
              clearable
              placeholder="密码"
            >
              <template #append>
                <el-button
                  :icon="showPwd ? 'Hide' : 'View'"
                  @click="showPwd = !showPwd"
                />
              </template>
            </el-input>
          </el-form-item>
        </el-form>
        <br />
        <el-button type="primary" @click="loginSys">登录</el-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ElMessageBox, type FormInstance } from "element-plus";
import { useRouter } from "vue-router";
import { reactive, ref } from "vue";
import { Login } from "../../services/base.api";
import { useAccountStore } from "../../stores/account";

const router = useRouter();

const accountStore = useAccountStore();

/* Form 参数 */
const formRef = ref<FormInstance>();
const forms = reactive({
  account: "",
  password: "",
});
const showPwd = ref(false);
/* 检验规则 */
const rules = {
  account: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};

/* 登录系统 */
const loginSys = () => {
  formRef.value?.validate((valid: any) => {
    if (valid) {
      const params = { ...forms };
      Login(params)
        .then((res: any) => {
          const { userinfo } = res.data;
          if (userinfo && userinfo.userId) {
            // 0: 用户 1：管理员
            if (userinfo.role == 1) {
              accountStore.setAccount(userinfo);
              router.replace("/main");
            } else {
              ElMessageBox({
                title: "提示",
                message: "您没有权限登录系统，请联系管理员",
              })
                .then(() => {})
                .catch(() => {});
            }
          } else {
            ElMessageBox({
              title: "提示",
              message: "登录失败，请检查账号密码是否正确",
            })
              .then(() => {})
              .catch(() => {});
          }
        })
        .finally(() => {});
    }
  });
};
</script>

<style scoped lang="scss">
.login {
  width: 100%;
  height: 100%;
  background-color: #1b1f24;
  background-repeat: no-repeat;
  background-size: 1920px 100%;
  background-position-x: center;
  position: relative;

  &::after {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin-left: auto;
    margin-right: auto;
    content: "";
    background-image: url(@/assets/images/bg-pattern.png);
    background-size: 100%;
    mix-blend-mode: screen;
    opacity: 50%;
    pointer-events: none;
    z-index: 0;
  }

  .overlay {
    pointer-events: none;
    transform: translateZ(0);
    filter: blur(96px);
    opacity: 0.5;
    background-image: linear-gradient(
      to bottom right,
      #79c0ff,
      #84edc1,
      rgba(121, 192, 255, 0)
    );
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 0;
  }

  .cont {
    width: 320px;
    padding: 20px 30px 30px;
    background-color: #ffffff;
    border-radius: 4px;
    transform: translate(-50%, -50%);
    position: absolute;
    left: 50%;
    top: 45%;

    .title {
      text-align: center;
      font-size: 24px;
      font-weight: bold;
      margin: 10px auto 30px;
    }

    .form {
      > .el-button {
        width: 100%;
      }

      .code {
        line-height: 0;
      }
    }
  }
}
</style>
